package com.jichangxiu.framework.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.jichangxiu.common.context.JcxContext;
import com.jichangxiu.common.entity.bo.Payload;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.entity.vo.Result;
import com.jichangxiu.common.enums.InfoType;
import com.jichangxiu.common.utils.IpUtils;
import com.jichangxiu.framework.entity.bo.SecurityToken;
import com.jichangxiu.framework.entity.bo.SecurityUser;
import com.jichangxiu.common.entity.rule.InfoRule;

import javax.servlet.http.HttpServletRequest;

public class InfoRuleUtils {

    public static final String LOGIN_INFO_RECORD = "LOGIN_INFO_RECORD";

    public static InfoRule getLoginInfoRule(HttpServletRequest httpServletRequest, SecurityToken securityToken) {
        InfoRule infoRule = new InfoRule();
        infoRule.setTenantId(String.valueOf(securityToken.getTenantId()));
        infoRule.setUserId(String.valueOf(securityToken.getPrincipal()));
        infoRule.setType(InfoType.LOGIN.name());
        infoRule.setAddress(IpUtils.getClientIp(httpServletRequest));
        infoRule.setBrowser(IpUtils.getClientBrowser(httpServletRequest));
        infoRule.setSystem(IpUtils.getClientOs(httpServletRequest));
        infoRule.setParam(JSONUtil.toJsonStr(Record.getRequestRecord(httpServletRequest)));
        infoRule.setCreateBy(String.valueOf(securityToken.getPrincipal()));
        infoRule.setUpdateBy(String.valueOf(securityToken.getPrincipal()));
        return infoRule;
    }

    public static void fillSuccessLoginInfoRule(InfoRule infoRule, Payload<SecurityUser> payload, Result<Object> result) {
        if (ObjectUtil.isNotEmpty(infoRule)) {
            infoRule.setStatus(true);
            infoRule.setException("{}");
            if (ObjectUtil.isNotEmpty(payload)) {
                infoRule.setJti(payload.getJti());
                infoRule.setJwt(payload.getJwt());
            } else {
                String jti = JcxContext.getJti();
                if (StrUtil.isNotEmpty(jti)) {
                    infoRule.setJti(jti);
                }
                String jwt = JcxContext.getJwt();
                if (StrUtil.isNotEmpty(jwt)) {
                    infoRule.setJwt(jwt);
                }
            }
            if (ObjectUtil.isNotEmpty(result)) {
                infoRule.setResult(JSONUtil.toJsonStr(result));
            } else {
                infoRule.setResult(JSONUtil.toJsonStr(Result.no("用户登录成功")));
            }
            SecurityUser userInfo = payload.getUserInfo();
            if (ObjectUtil.isNotEmpty(userInfo)) {
                infoRule.setUserId(userInfo.getUserId());
                infoRule.setDeptBy(userInfo.getDeptId());
                infoRule.setCreateBy(userInfo.getUserId());
                infoRule.setUpdateBy(userInfo.getUserId());
            } else {
                SecurityUser securityUser = JcxContext.getSecurityUser(SecurityUser.class);
                if (ObjectUtil.isNotEmpty(securityUser)) {
                    infoRule.setUserId(securityUser.getUserId());
                    infoRule.setDeptBy(securityUser.getDeptId());
                    infoRule.setCreateBy(securityUser.getUserId());
                    infoRule.setUpdateBy(securityUser.getUserId());
                } else {
                    infoRule.setDeptBy(JcxContext.getDeptId());
                    String userId = JcxContext.getUserId();
                    infoRule.setUserId(userId);
                    infoRule.setCreateBy(userId);
                    infoRule.setUpdateBy(userId);
                }
            }
        }
    }

    public static void fillFailLoginInfoRule(InfoRule infoRule, Exception ex, Result<Object> result) {
        if (ObjectUtil.isNotEmpty(infoRule)) {
            infoRule.setStatus(false);
            String jti = JcxContext.getJti();
            if (StrUtil.isNotEmpty(jti)) {
                infoRule.setJti(jti);
            } else {
                infoRule.setJti("登录失败");
            }
            String jwt = JcxContext.getJwt();
            if (StrUtil.isNotEmpty(jwt)) {
                infoRule.setJwt(jwt);
            } else {
                infoRule.setJwt("登录失败");
            }
            if (ObjectUtil.isNotEmpty(ex)) {
                infoRule.setException(JSONUtil.toJsonStr(ex));
            }
            if (ObjectUtil.isNotEmpty(result)) {
                infoRule.setResult(JSONUtil.toJsonStr(result));
            } else {
                infoRule.setResult(JSONUtil.toJsonStr(Result.no("用户登录失败")));
            }
            SecurityUser securityUser = JcxContext.getSecurityUser(SecurityUser.class);
            if (ObjectUtil.isNotEmpty(securityUser)) {
                infoRule.setUserId(securityUser.getUserId());
                infoRule.setDeptBy(securityUser.getDeptId());
                infoRule.setCreateBy(securityUser.getUserId());
                infoRule.setUpdateBy(securityUser.getUserId());
            } else {
                infoRule.setDeptBy(JcxContext.getDeptId());
                String userId = JcxContext.getUserId();
                infoRule.setUserId(userId);
                infoRule.setCreateBy(userId);
                infoRule.setUpdateBy(userId);
            }
        }
    }

    public static void fillLocation(InfoRule infoRule) {
        if (ObjectUtil.isNotEmpty(infoRule) && StrUtil.isNotEmpty(infoRule.getAddress())) {
            infoRule.setLocation(IpUtils.getRealAddress(infoRule.getAddress()));
        }
    }

    public static InfoRule getLogoutInfoRule(HttpServletRequest httpServletRequest) {
        InfoRule infoRule = new InfoRule();
        infoRule.setStatus(true);
        infoRule.setType(InfoType.LOGOUT.name());
        infoRule.setException("{}");
        infoRule.setAddress(IpUtils.getClientIp(httpServletRequest));
        infoRule.setBrowser(IpUtils.getClientBrowser(httpServletRequest));
        infoRule.setSystem(IpUtils.getClientOs(httpServletRequest));
        infoRule.setParam(JSONUtil.toJsonStr(Record.getRequestRecord(httpServletRequest)));
        Payload<SecurityUser> payload = SecurityUtils.getPayload(httpServletRequest);
        if (ObjectUtil.isEmpty(payload)) {
            infoRule.setJti(payload.getJti());
            infoRule.setJwt(payload.getJwt());
            fillLogoutInfo(infoRule, payload.getUserInfo());
        } else {
            String jti = JcxContext.getJti();
            if (StrUtil.isNotEmpty(jti)) {
                infoRule.setJti(jti);
            }
            String jwt = JcxContext.getJwt();
            if (StrUtil.isNotEmpty(jwt)) {
                infoRule.setJwt(jwt);
            }
            fillLogoutInfo(infoRule, JcxContext.getSecurityUser(SecurityUser.class));
        }
        return infoRule;
    }

    private static void fillLogoutInfo(InfoRule logoutInfoRule, SecurityUser securityUser) {
        if (ObjectUtil.isNotEmpty(securityUser)) {
            logoutInfoRule.setTenantId(String.valueOf(securityUser.getTenantId()));
            logoutInfoRule.setUserId(String.valueOf(securityUser.getUserId()));
            logoutInfoRule.setDeptBy(String.valueOf(securityUser.getDeptId()));
            logoutInfoRule.setCreateBy(String.valueOf(securityUser.getUserId()));
            logoutInfoRule.setUpdateBy(String.valueOf(securityUser.getUserId()));
        } else {
            String tenantId = JcxContext.getTenantId();
            if (StrUtil.isNotEmpty(tenantId)) {
                logoutInfoRule.setTenantId(tenantId);
            }
            String userId = JcxContext.getUserId();
            if (StrUtil.isNotEmpty(userId)) {
                logoutInfoRule.setUserId(userId);
                logoutInfoRule.setCreateBy(userId);
                logoutInfoRule.setUpdateBy(userId);
            }
            String deptId = JcxContext.getDeptId();
            if (StrUtil.isNotEmpty(deptId)) {
                logoutInfoRule.setDeptBy(deptId);
            }
        }
    }

}
